<?php
/**
 * @author jonathan poelen <jonathan.poelen@gmail.com>
 * @version 1.0: jo_link_noir
 * @date 27-05-2010
 */
class SW_Structure_Subject_Element extends SW_Structure_Element implements SW_I_Element_Subject
{
	/** @var array : {SW_I_Element_Observer, ...}*/
	protected $storage = array();

	/**
	 * @param int $level
	 */
	public function setLevel($level)
	{
		parent::setLevel($level);
		$this->notify();
	}

	/**
	 * @param SW_I_Element_Observer $observer
	 */
	public function attach(SW_I_Element_Observer $observer)
	{
		if (false === array_search($observer, $this->storage, true))
			$this->storage[] = $observer;
	}

	/**
	 * @param SW_I_Element_Observer $observer
	 */
	public function detach(SW_I_Element_Observer $observer)
	{
		if (false !== ($key = array_search($observer, $this->storage, true)))
			unset($this->storage[$key]);
	}

	/**
	 * Lance les observateur associé
	 */
	public function notify()
	{
		foreach ($this->storage as $o)
			$o->update($this);
	}
}